﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;

namespace Chaks.SharePoint.Docs.ILP.Utilities
{
    public class ILPRoleInheritanceJob : SPJobDefinition
    {
        private const string EventReceiverAssembly = "Chaks.SharePoint.Docs.ILP, Version=1.1.0.0, Culture=neutral, PublicKeyToken=5e4a77ec46c0cc45";
        private const string EventReceiverClass = "Chaks.SharePoint.Docs.ILP.EventReceivers.DocumentLibraryEventReceiver";

        public ILPRoleInheritanceJob()
        {
            
        }

        public ILPRoleInheritanceJob(string jobName,SPService spService,SPServer spServer,SPJobLockType spJobLockType)
            :base(jobName,spService,spServer,spJobLockType)
        {
            
        }

        public ILPRoleInheritanceJob(string jobName,SPWebApplication spWebApplication)
            :base(jobName,spWebApplication,null,SPJobLockType.ContentDatabase)
        {
            
        }

        public override void Execute(Guid targetInstanceId)
        {
            string ilpJob = Properties["ILP_Job"].ToString();
            Guid siteId = new Guid(Properties["ILP_SiteId"].ToString());
            Guid webId = new Guid(Properties["ILP_WebId"].ToString());
            Guid listId = new Guid(Properties["ILP_ListId"].ToString());

            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                using (SPSite spSite = new SPSite(siteId))
                {
                    using (SPWeb spWeb = spSite.OpenWeb(webId))
                    {
                        
                        SPList docList = spWeb.Lists.GetList(listId, false);
                        SPListItem listItem=null;

                        SPEventReceiverDefinition itemUpdatedEvent = docList.EventReceivers.Cast<SPEventReceiverDefinition>()
                                                                 .FirstOrDefault(listEvent => listEvent.Assembly == EventReceiverAssembly
                                                                               && listEvent.Type == SPEventReceiverType.ItemUpdated);

                        SPEventReceiverDefinition itemAddedEvent = docList.EventReceivers.Cast<SPEventReceiverDefinition>()
                                                                                 .FirstOrDefault(listEvent => listEvent.Assembly == EventReceiverAssembly
                                                                                               && listEvent.Type == SPEventReceiverType.ItemAdded);

                        if (ilpJob == "reset")
                        {
                            for (int i = 0; i < docList.Items.Count; i++)
                            {
                                listItem = docList.Items[i];
                                listItem.ResetRoleInheritance();
                            }
                        }
                        else if (ilpJob == "break")
                        {
                            for (int i = 0; i < docList.Items.Count; i++)
                            {
                                listItem = docList.Items[i];
                                listItem.BreakRoleInheritance(true);
                            }
                        }

                        docList.Update();
                    }
                }

                // finally delete the timer job
                Delete();
            });
        }
    }
}
